//
//  AppCongifure.m
//  CedarFinancePH
//
//  Created by 门超 on 2018/9/2.
//  Copyright © 2018年 cedarhd. All rights reserved.
//

#import "AppCongifure.h"

static NSString * const kAppLoginTokenKey = @"appLoginTokenKey";
static NSString * const kAppVersionKey = @"AppVersionKey";
static NSString * const kAppUpdateTimeKey = @"AppUpdateTimeKey";
static NSString * const kSyncUserDataKey = @"SyncUserDataKey";
static NSString * const kAppFirstRunningKey = @"AppFirstRunninKey";
static NSString * const kAppDeviceTokenKey = @"AppDeviceTokenKey";
static NSString * const kHomeActivityIdKey = @"HomeActivityIdKey";
static NSString * const kDisclaimerTipsKey = @"DisclaimerKey";


@interface AppCongifure ()



@end

@implementation AppCongifure

//@synthesize deviceTokenFromCache = _deviceTokenFromCache;


+ (instancetype)shareInstance{
    static AppCongifure *dataBaseStore = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate, ^{
        dataBaseStore = [[AppCongifure alloc] init];
    });
    return dataBaseStore;
}



- (void)saveAppLoginToken:(NSString *)appLoginToken{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    if(appLoginToken){
        [userDefault setObject:appLoginToken forKey:kAppLoginTokenKey];
    }else{
        [userDefault removeObjectForKey:kAppLoginTokenKey];
    }
    [userDefault synchronize];
}

- (NSString *)appLoginToken{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:kAppLoginTokenKey];
}

- (void)saveAppVersion:(NSString *)appVersion{
     NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    if(appVersion){
        [userDefault setObject:appVersion forKey:kAppVersionKey];
    }else{
        [userDefault removeObjectForKey:kAppVersionKey];
    }
     [userDefault synchronize];
}

- (NSString *)appVersion{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:kAppVersionKey];
}

- (void)saveAppUpdateTime:(NSString *)appUpdateTime{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    if(appUpdateTime){
        [userDefault setObject:appUpdateTime forKey:kAppUpdateTimeKey];
    }else{
        [userDefault removeObjectForKey:kAppUpdateTimeKey];
    }
    [userDefault synchronize];
}

- (NSString *)appUpdateTime{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    return [userDefault objectForKey:kAppUpdateTimeKey];
}

- (void)saveIsSyncUserData:(BOOL)isSyncUserData{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSNumber *number = [NSNumber numberWithBool:isSyncUserData];
    [userDefault setObject:number forKey:kSyncUserDataKey];
    [userDefault synchronize];
}

- (BOOL)isSyncUserData{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSNumber *number = [userDefault objectForKey:kSyncUserDataKey];
    if (number) {
        return  number.boolValue;
    }
    return NO;
}

- (BOOL)isAppFirstRunning{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    BOOL isFirst = [userDefault boolForKey:kAppFirstRunningKey];
    return isFirst;
}

- (void)setAppFirstRunning:(BOOL)isFirst{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setBool:isFirst forKey:kAppFirstRunningKey];
        [userDefault synchronize];
    });
}

#pragma mark - home activity

- (void)setHomeActivityId:(NSString *)homeActivityId{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        if (!homeActivityId) {
            return;
        }
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setObject:homeActivityId forKey:kHomeActivityIdKey];
        [userDefault synchronize];
    });
}

- (NSString *)homeActivityId{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSString *activityId = [userDefault objectForKey:kHomeActivityIdKey];
    return activityId;
}



#pragma mark - devieceToken


- (void)setDeviceToken:(NSString *)deviceToken{
    _deviceToken = deviceToken;
    if(deviceToken){
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setObject:deviceToken forKey:kAppDeviceTokenKey];
        [userDefault synchronize];
    }
}

//- (NSString *)deviceToken{
//    if(!_deviceToken){
//        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
//        _deviceToken = [userDefault objectForKey:kAppDeviceTokenKey];
//    }
//    return _deviceToken;
//}

- (NSString *)deviceTokenFromCache{
    if(!_deviceTokenFromCache){
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        _deviceTokenFromCache = [userDefault objectForKey:kAppDeviceTokenKey];
    }
    return _deviceTokenFromCache;
}


/*
static NSString * const kLoginMobileKey = @"LoginMobileKey";
 
- (void)setDeviceToken:(NSString *)deviceToken{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setObject:deviceToken forKey:kAppDeviceTokenKey];
        [userDefault synchronize];
    });
}

- (NSString *)deviceToken{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSString *deviceToken = [userDefault objectForKey:kAppDeviceTokenKey];
    return deviceToken;
}


- (void)setLoginMobile:(NSString *)loginMobile{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setObject:loginMobile forKey:kLoginMobileKey];
        [userDefault synchronize];
    });
}

- (NSString *)loginMobile{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSString *mobile = [userDefault objectForKey:kLoginMobileKey];
    return mobile;
}*/


//免责声明是否显示
- (void)setDisclaimerTips:(BOOL)set{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    [userDefault setBool:set forKey:kDisclaimerTipsKey];
    [userDefault synchronize];
}

- (BOOL)isShowDisclaimerTips{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    BOOL isShow = [userDefault boolForKey:kDisclaimerTipsKey];
    return isShow;
}

#pragma mark -

+ (void)saveData:(NSString *)data key:(NSString *)key{
    if(data && key){
        NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
        [userDefault setObject:data forKey:key];
        [userDefault synchronize];
    }
}

+ (NSString *)dataByKey:(NSString *)key{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSString *data = [userDefault objectForKey:key];
    return data;
}

- (void)saveAchievementPreview{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSNumber *number = [NSNumber numberWithBool:YES];
    [userDefault setObject:number forKey:kAchievementPreviewKey];
    [userDefault synchronize];
}

- (BOOL)isShowAchievementPreview{
    NSUserDefaults *userDefault =   [NSUserDefaults standardUserDefaults];
    NSNumber *number = [userDefault objectForKey:kAchievementPreviewKey];
    if (number) {
        return  number.boolValue;
    }
    return NO;
}


@end
